//
//  DMAccountTool.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/7/21.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "DMAccountTool.h"
#import "Account.h"

@implementation DMAccountTool

+ (BOOL)saveAccount:(Account *)account
{
    
    // 2归档对象
    return [NSKeyedArchiver archiveRootObject:account toFile: AccountFilePath];
}

+ (Account *)account
{
    
    // 2.读取账号模型
    Account *account = [NSKeyedUnarchiver unarchiveObjectWithFile: AccountFilePath];
    return account;
}
#pragma 保存数据到NSUserDefaults
+(void)writeAccount:(Account *)account
{
    NSUserDefaults * userDefaulte = [NSUserDefaults standardUserDefaults];
    [userDefaulte setObject:account.phone forKey:@"phone"];
    [userDefaulte setObject:account.userId forKey:@"userId"];
    [userDefaulte setObject:account.avatar forKey:@"avatar"];
    [userDefaulte setObject:account.nickname forKey:@"nickname"];
    [userDefaulte synchronize];
    
}

#pragma 从NSUserDefaults读取数据
+(Account *)readAccount
{
    NSUserDefaults * userDefaulte = [NSUserDefaults standardUserDefaults];
    
    NSString * authenticationType = [userDefaulte stringForKey:@"authenticationType"];
    if (authenticationType == 0) {
        authenticationType = @"未认证";
    }
    
    NSString * authenticationAs = [userDefaulte stringForKey:@"authenticationAs"];
    if (authenticationAs == 0 ) {
        authenticationAs = @"0";
    }
    
    NSString * phone = [userDefaulte stringForKey:@"phone"];
    NSString * userId = [userDefaulte stringForKey:@"userId"];
    NSString * avatar =[userDefaulte stringForKey:@"avatar"];
    NSString * nickname =[userDefaulte stringForKey:@"nickname"];
    
    
    NSMutableDictionary * md =[NSMutableDictionary dictionary];
    
    md[@"authenticationType"]=authenticationType;
    md[@"authenticationType"]=authenticationType;
    md[@"phone"]=phone;
    md[@"userId"]=userId;
    md[@"avatar"] = avatar;
    md[@"nickname"] = nickname;
    
    Account * account = [Account accountWithDict:md];
    return account;
    
    
}


@end
